// ==UserScript==
// @name         kaikeba-autoplay
// @namespace    http://tampermonkey.net/
// @version      0.1
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @description  try to take over the world!
// @author       Sleeber.Ann
// @match        https://*.kaikeba.com/
// @match        https://*.kaikeba.com/*
// @grant        unsafeWindow
// @grant        window.close
// @grant        window.focus
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

const courseVip = ["Java全栈高级开发工程师", "职业精英实战特训营-001期", "百万架构师011期"];
const LAST_CHAPTER = "last_chapter";
const NOW_CHAPTER = "now_CHAPTER";
const LAST_SECTION = "last_section";
const NOW_SECTION = "now_SECTION";

//首页自动跳转到目录
function delayRedirectIndex(jQuery) {
    console.log(">>>1. index redirecting...");
    let $parentDiv = $("#KKB_header_box div.KKb_header_right_item");
    let $redirectBtn = $("#KKB_header_box div.KKb_header_right_item");
    if ("undefined" != typeof $
        && $parentDiv.length == 1
        && $redirectBtn.length == 1) {
        $redirectBtn.click();

        window.opener = null;
        window.close();
    } else {
        var delay01 = setTimeout(function () {
            delayRedirectIndex(jQuery);
        }, 1000);
    }
}

//目录自动跳转到指定课程
function delayRedirectCourse(jQuery) {
    console.log(">>>2. course redirecting...");
    let $parentDiv = $("#app div.home-nav div.main div.dake-area");
    let $courseList = $parentDiv.find("div.dake");
    if ("undefined" != typeof $ && $parentDiv.length == 1 && $courseList.length > 0) {
        $courseList.each(function (index, element) {
            var courseTitle = $.trim($(element).find("div.limit-word").text())
            if (courseVip[0] == courseTitle) {
                $(element).find("button").click();
            }
        });

        var delay02 = setTimeout(function () {
            console.log(">>>2.1. 准备跳转到指定章节...");
            delayRedirectChapter(jQuery);
        }, 10000);
    } else {
        var delay03 = setTimeout(function () {
            delayRedirectCourse(jQuery);
        }, 1000);
    }
}

//课程自动跳转到指定章节
function delayRedirectChapter(jQuery) {
    console.log(">>>3. chapter redirecting...");
    let $parentDiv = $("#app div.main div.main-inner div.ivu-collapse");
    let $chapterList = $parentDiv.children(".ivu-collapse-item");

    if ("undefined" != typeof $ && $parentDiv.length == 1 && $chapterList.length > 0) {
        for (let index = 0; index < $chapterList.length; index++) {
            let element = $chapterList[index];
            let $chapter = $(element).find("div.title");

            if (GM_getValue(LAST_CHAPTER) === "" || GM_getValue(LAST_CHAPTER) >= $chapterList.length) {
                GM_setValue(LAST_CHAPTER, 0);
            }

            if (index == GM_getValue(LAST_CHAPTER)) {
                GM_setValue(NOW_CHAPTER, $chapter.text());
                GM_setValue(LAST_CHAPTER, -1 * index);
                if (!$(element).hasClass("ivu-collapse-item-active")) {
                    $chapter.click();
                }
                console.log(">>>3.1. 准备跳转到指定课节...");
                delayRedirectSection(jQuery, element);
                break;
            } else if (index == GM_getValue(LAST_CHAPTER) * -1) {
                if (!$(element).hasClass("ivu-collapse-item-active")) {
                    $chapter.click();
                }
                console.log(">>>3.2. 准备跳转到指定课节...");
                delayRedirectSection(jQuery, element);
                break;
            }
        }
    } else {
        var delay07 = setTimeout(function () {
            delayRedirectChapter(jQuery);
        }, 1000);
    }
}

//加载指定章节的课节
function delayRedirectSection(jQuery, element) {
    console.log(">>>4. section redirecting...");
    let $sectionList = $(element).find("ul li div.ivu-timeline-item-content");

    if ("undefined" != typeof $ && $sectionList.length > 0) {
        let noSelected = false;
        for (let index = 0; index < $sectionList.length; index++) {
            const element = $sectionList[index];

            if (GM_getValue(LAST_SECTION) === "" || GM_getValue(LAST_SECTION) < 0 || GM_getValue(LAST_SECTION) >= $sectionList.length) {
                GM_setValue(LAST_SECTION, -1);
            }

            if (index == (GM_getValue(LAST_SECTION) + 1)
                && ($(element).find("span.item-txt").text() === "看点播" || $(element).find("span.item-txt").text() === "看回放")) {
                GM_setValue(NOW_SECTION, $(element).find("p.contont-title").text())
                GM_setValue(LAST_SECTION, -1 * index);
                console.log(">>>4.1. 准备跳转到指定课节...");
                $(element).find("p.contont-title").click();
                break;
            } else if (index == $sectionList.length - 1) {
                noSelected = true;
            }
        }

        if (noSelected) {
            GM_setValue(LAST_SECTION, "");
            GM_setValue(LAST_CHAPTER, -1 * GM_getValue(LAST_CHAPTER) + 1);

            var delay08 = setTimeout(function () {
                console.log(">>>3.0. 课节自动播放及返回...");
                delayRedirectChapter(jQuery);
            }, 10000);

        } else {
            var delay05 = setTimeout(function () {
                console.log(">>>5.0. 课节自动播放及返回...");
                delayPlaySection(jQuery);
            }, 10000);
        }

    } else {
        var delay06 = setTimeout(function () {
            delayRedirectSection(jQuery, element);
        }, 1000);
    }
}

//课节自动播放及返回
function delayPlaySection(jQuery) {
    console.log(">>>5. 正在播放：" + GM_getValue(NOW_CHAPTER) + " --> " + GM_getValue(NOW_SECTION));

    let $returnBtn = $("#app div.video-main div.video-content div.palyback-end div.btn");
    if ("undefined" != typeof $ && $returnBtn.length == 1) {
        GM_setValue(LAST_SECTION, GM_getValue(LAST_SECTION) * -1);

        $returnBtn.click();
    } else {
        var delay04 = setTimeout(function () {
            delayPlaySection(jQuery);
        }, 10000);
    }
}


const url = window.location.href;
if ("https://www.kaikeba.com/" == url) {
    //首页自动跳转到目录
    console.log("准备跳转到学习中心...");
    delayRedirectIndex(jQuery);
} else if (url.startsWith("https://learn.kaikeba.com/home")) {
    //目录自动跳转到指定课程
    console.log("准备跳转到指定课程...");
    delayRedirectCourse(jQuery);
} else if (url.startsWith("https://learn.kaikeba.com/catalog")) {
    //课程自动跳转到指定章节
    console.log("准备跳转到指定章节...");
    delayRedirectChapter(jQuery);
} else if (url.startsWith("https://learn.kaikeba.com/video")) {
    //课程自动跳转到指定课节
    console.log("准备播发到指定课节...");
    delayPlaySection(jQuery);
}


// GM_setValue(NOW_SECTION, "");
// GM_setValue(LAST_SECTION, "");
// GM_setValue(NOW_CHAPTER, "");
// GM_setValue(LAST_CHAPTER, "");

